package ch.uzh.ifi.seal.ssqa.popup.actions;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;

import ch.uzh.ifi.seal.ssqa.SsqaPlugin;
import ch.uzh.ifi.seal.ssqa.preferences.PreferenceConstants;

public class ImportProjectsAction implements IWorkbenchWindowActionDelegate {

	private IWorkbenchWindow window;

	public ImportProjectsAction() {

	}

	@Override
	public void run(IAction action) {
		IRunnableWithProgress r = new IRunnableWithProgress() {

			@Override
			public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException {
				final IWorkspace workspace = ResourcesPlugin.getWorkspace();
				// TODO: put dirname to preference pages
				//final String dirname = "Z:\\CCFS\\src";
				final IPreferenceStore store = SsqaPlugin.getDefault().getPreferenceStore();
				final String dirname = store.getString(PreferenceConstants.CC_VIEW_DEVICE) 
						+ ":" + store.getString(PreferenceConstants.CC_PATH_TO_SOURCE);

				
				File file = new File(dirname);
				String[] directories = file.list(new FilenameFilter() {
					@Override
					public boolean accept(File dir, String name) {
						// TODO Only first depth sub folder
						File subDir = new File(dir.getPath() + "\\" + name);
						return subDir.isDirectory();
					}
					
					
				});
				// System.out.println(Arrays.toString(directories));

				if (directories != null && directories.length != 0) {
					for (final String dir : directories) {
						final IProgressMonitor monitorCopy = monitor;
						Runnable runnable = new Runnable() {
							public void run() {								
									IPath path = new Path(dirname + "\\" + dir + "/.project");
									File file = path.toFile();
									if(file.exists()){										
										IProjectDescription ds;
										try {
											ds = workspace
													.loadProjectDescription(path);
											IProject project = workspace.getRoot()
													.getProject(dir);
											if(!project.exists()){
												JavaCapabilityConfigurationPage
												.createProject(project,
														ds.getLocationURI(),
														monitorCopy);
											}
											
										} catch (CoreException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
										
									}else{
										System.out.println(path.toOSString() + " not Found");
										return;
									}
									
								
							}
						};
						final IWorkbench wb = PlatformUI.getWorkbench();
						wb.getDisplay().syncExec(runnable);
					}
				}
				monitor.done();
			}

		};
		
		IWorkbench wb = PlatformUI.getWorkbench();
		IProgressService ps = wb.getProgressService();
		try {
			ps.busyCursorWhile(r);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IWorkbenchWindow window) {
		this.window = window;

	}

}
